<script>
import { mapMutations, mapState } from "vuex"
import storage from "@/uni-sub-base/utils/storage.js"
import store from "@/store/index.js"
import config from "@/config/config.js"
export default {
    data() {
        return {
            // 记录返回键被点击的次数
            backClickCount: 0,
            // 存储定时器
            timer: null,
            url: '/pages/login/loading',
        };
    },
    onShow() {
        // 监听返回键事件
        // #ifdef APP-PLUS
        uni.addEventListener('backbutton', this.onBackButton);
        // #endif
    },
    computed: {
        ...mapState(["networkType"]),
    },
    onLaunch: function (options) {
        console.log("App Launch", options)
        if (options.query.isFromRegister) {
            this.url = this.url + "?isFromRegister=" + options.query.isFromRegister
        }
        if (options.query.isScanCodeEnter) {
            this.url = this.url + "?isScanCodeEnter=" + options.query.isScanCodeEnter
        }
        if (options.query.invitUserPhone) {
            this.url = this.url + "?invitUserPhone=" + options.query.invitUserPhone
        }
        this.initNetworkType();
        //#ifdef APP-PLUS
        switch (uni.getSystemInfoSync().platform) {
            case "android":
                //console.log('运行Android上')
                plus.navigator.closeSplashscreen()
                break
            case "ios":
                //console.log('运行iOS上')
                plus.navigator.closeSplashscreen()
                break
            default:
                //console.log('运行在开发者工具上')
                break
        }
        // #endif
        this.h5Init() //主题色
        this.handleLaunchOptions(options)
        // this.appLoginInit()
        // this.GetGuideApi()//接口获取API地址
    },
    onHide: function () {
        console.log("App Hide")
        store.commit('SKIP_RESET')
        // 移除监听
        uni.removeEventListener('backbutton', this.onBackButton);
        // 清除定时器
        clearTimeout(this.timer);
        uni.offNetworkStatusChange();
        this.SET_NETWORK_TYPE("none")
    },
    methods: {
        ...mapMutations(['SET_NETWORK_TYPE']),
        initNetworkType() {
            let that = this;
            //获取网络类型
            uni.getNetworkType({
                success: async function (res) {
                    that.SET_NETWORK_TYPE(res.networkType)
                    uni.hideLoading()
                    if (res.networkType == 'none') {
                        // 无网络
                        console.log('无网络')
                    } else {
                        console.log('有网络1')
                        await that.GetGuideApi()//接口获取API地址
                        uni.reLaunch({
                            url: that.url,
                        })
                    }
                }
            })

            //网络类型变化监听
            uni.onNetworkStatusChange(async function (res) {
                that.SET_NETWORK_TYPE(res.networkType)
                uni.hideLoading()
                if (res.networkType != "none") {
                    console.log('有网络2')
                    await that.GetGuideApi()//接口获取API地址
                    uni.reLaunch({
                        url: that.url,
                    })
                }
            })

        },
        appLoginInit() {
            console.log("判断登录状态")
            //判断登录状态
            let token = storage.getCache("token")
            if (!token || token == "") {
                //未登录
                let url = "/pages/login/loading"
                uni.reLaunch({
                    url: url,
                })
            }
        },
        handleLaunchOptions(options) {
            if (options.query.tel) {
                storage.setCache('inviteTel', options.query.tel);
            }
            // 判断是否是通过分享进入  
            if (options.scene === 1007 || options.scene === 1008 || options.scene === 1044 || options.scene === 1074) { // 1035 是从另一个小程序或公众号分享消息卡片进入小程序的场景值  
                // 用户是通过分享进入的，你可以在这里做一些处理，比如设置全局状态  
                wx.setStorageSync('isFromShare', true);
            }
        },
        //#ifdef APP-PLUS
        onBackButton() {
            // 第一次点击返回键，提示“再次点击退出”
            if (this.backClickCount === 0) {
                this.$toast('再次点击退出应用');
                this.backClickCount++;
                // 启动定时器，若用户不再点击，重置点击次数
                this.timer = setTimeout(() => {
                    this.backClickCount = 0;
                }, 2000); // 2秒内没有再次点击则重置
            } else {
                // 第二次点击，退出应用
                plus.runtime.quit();
            }
        }
        // #endif
    },
}
</script>

<style lang="scss">
@import "@/style/common.scss";
@import "@/style/btn.scss";
@import "@/style/icon.scss";
@import "@/style/pop.scss";
@import "@/style/iconfont/iconfont.css";
@import "@/style/drawer.scss";

.custom-checkbox-box {
    position: relative;

    .checkbox-mark {
        position: absolute;
        z-index: 0;
        opacity: 0;
    }
}

.placeholderClass {
    color: #bcbfcc;
    font-size: $font-size-30;
}

.placeholderSearchClass {
    color: #b3b8bd;
    font-size: $font-size-26;
}

.placeholderSearchClass1 {
    color: #b3b8bd;
    font-size: 12px;
}

.popupright .uni-popup__wrapper {
    width: 750rpx;
}

.posicons {
    position: absolute;
    bottom: -18rpx;
    right: 0rpx;
    transform: scale(0.8);
    z-index: 10000;
    position: absolute;
}

//#ifdef MP
.hx-navbar__content__main {
    max-width: calc(100vw) !important;
}

.hx-navbar__content__main_center {
    position: absolute;
    width: 100% !important;
    top: 0;
    justify-content: center !important;
}

//#endif
.product-main-nav .uni-scroll-view {
    overflow: visible !important;
}
</style>
